/**
 * 认证模块
 */
layui.define(function (exports) {
    layui.use(["layer", "element", 'setter', 'view', 'AppHelper'], function () {
        var layer = layui.layer;
        var setter = layui.setter;
        var request = setter.request
            , view = layui.view
            , AppHelper = layui.AppHelper
        ;
        var $ = layui.$;


        /**
         * 对外暴露的方法
         */
        exports("auth", {})

        //ajax设置
        $.ajaxSetup({
            cache: false,
            beforeSend: function (XHR) {
                console.log(XHR)
                XHR.setRequestHeader("Authorization", "Bearer " + (layui.data(setter.tableName)[request.tokenName] || ''));
            }
            , statusCode: {
                400: function (xhr) {
                    console.log("400:", xhr)
                    var res = xhr.responseJSON;
                    switch (res.code) {
                        case "110001":
                            layer.alert(res.message);
                            break;
                    }
                }
                , 401: function () {
                    var topWin = AppHelper.getTopWindow();
                    var host = AppHelper.getHost();
                    var jumpPath = host + "/" + setter.dir + "/user/login.html";
                    // var parent = window.parent;

                    topWin.location.href = jumpPath;
                    // if (parent) {
                    //     parent.location.href = jumpPath;
                    // } else {
                    //     location.href = jumpPath;
                    // }
                }
                , 403: function () {
                    var topWin = AppHelper.getTopWindow();
                    topWin.layui.layer.alert("您没有权限访问",{icon:0});
                }
            }
        });

        if (typeof juicer != "undefined") {
            juicer.register('clean', function (val) {
                return val ? val : "";
            });
        }
    });
});

